package services

import (
	"github.com/spf13/cast"
	"regexp"
	"strings"
)

type LoginRule struct {
}

// 前19个字符,日期时间
func (r *LoginRule) GetActionTime(str string) string {
	return str[:19]
}

// 玩家信息：playerName, playerId, playerIp, playerStreamId
func (r *LoginRule) GetPlayerInfo(str string) (string, string, string, string) {
	//分组匹配，匹配到第一个(符号就结束,在如上的字符中只匹配出‘服主徐迷儿’
	reg := regexp.MustCompile(`'([^']*)'`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return "无名字", "无id", "无ip", "无streamId"
	}
	loginTxt := strslice[1]
	//	空格分割
	strslice = strings.Split(loginTxt, " ")
	if len(strslice) < 2 {
		return "无名字", "无id", "无ip", "无streamId"
	} else {
		playerIp := strslice[0] //IP:
		strPlayerTxt := strings.Split(strslice[1], ":")
		playerStreamId := strPlayerTxt[0] //steamid
		playerTxt := strPlayerTxt[1]      //服主(180)
		reg1 := regexp.MustCompile(`\((.*?)\)`)
		strslice1 := reg1.FindStringSubmatch(playerTxt)
		if len(strslice1) == 0 {
			return "无名字", "无id", "无ip", "无streamId"
		}
		reg2 := regexp.MustCompile(`(.*?)\(`)
		strslice2 := reg2.FindStringSubmatch(playerTxt)
		if len(strslice2) == 0 {
			return "无名字", "无id", "无ip", "无streamId"
		}
		playerName, playerId := strslice2[1], strslice1[1]
		return playerName, playerId, playerIp, playerStreamId
	}
}

func (r *LoginRule) LoginType(str string) string {
	//正则以logged开头at:结尾，取中间的所有字符
	reg := regexp.MustCompile(`logged\s+(.*?)\sat:`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return "无法判断登录类型"
	}
	//strslice[1]去除前后空格
	logStr := strings.TrimSpace(strslice[1])
	return logStr
}

// 区域方法A0B0等等
func (r *LoginRule) GetLoginArea(str string) string {
	//1、获取Location:后面的所有字符
	loc := r.getLoginPosTxt(str)
	//2、将loc字符串分割为x和y
	//判断loc是否包含X=，如果不包含
	if !strings.Contains(loc, "X=") {
		return "无数据"
	}
	x, y := r.extractXY(loc)
	//3、根据x和y的值判断
	return r.getLockArea(x, y)
}

// 区域一: 后面的所有字符文本
func (r *LoginRule) getLoginPosTxt(str string) string {
	//正则匹配以at:开头后面的所有字符
	reg := regexp.MustCompile(`at: (.*)`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return ""
	}
	return strslice[1]
}

// 区域二： 空白分割，再以=分割，取第二个
func (r *LoginRule) extractXY(str string) (string, string) {
	//str为X=-863920.500 Y=-727241.875 Z=2226.781形式
	//1、以空白分割
	strslice := strings.Split(str, " ")
	//2、再以=分割
	x := strings.Split(strslice[0], "=")[1]
	y := strings.Split(strslice[1], "=")[1]
	return x, y
}

// 区域三：根据x和y的值判断
func (r *LoginRule) getLockArea(x string, y string) string {
	//1、将x和y转换为float64
	xfloat64 := cast.ToFloat64(x)
	yfloat64 := cast.ToFloat64(y)
	if 599996.750 > xfloat64 && xfloat64 > 904795.125 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A0"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A4"
	}
	if -599996.750 > xfloat64 && xfloat64 > -904795.125 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B1"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B4"
	}
	if -599996.750 > xfloat64 && xfloat64 > -904795.125 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C0"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C4"
	}

	if -599996.750 > xfloat64 && xfloat64 > -904795.125 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D0"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D4"
	}

	if -599996.750 > xfloat64 && xfloat64 > -904795.125 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z0"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z4"
	}
	//2、判断x和y的范围
	//3、返回对应的区域
	return ""
}

func (r *LoginRule) GetLoginPosition(str string) string {
	getLoginPosTxt := r.getLoginPosTxt(str)
	if !strings.Contains(getLoginPosTxt, "X=") {
		return "无数据"
	}
	strX, strY := r.extractXY(getLoginPosTxt)
	return strX + "," + strY
}
